package com.wudl.windows;

import com.wudl.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * /**
 *
 * @ClassName : Window_Watermark_AllowerLaster
 * @Description : 对于窗口延迟的数据处理
 * @Author :wudl
 * @Date: 2020-10-28 23:44
 */

public class Window_Watermark_AllowerLaster {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 指定时间语义
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        SingleOutputStreamOperator<WaterSensor> sensorDs = env.socketTextStream("192.168.1.180", 8899)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new WaterSensor(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2]));
                    }
                }).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<WaterSensor>(Time.seconds(3)) {
                                                     @Override
                                                     public long extractTimestamp(WaterSensor element) {
                                                         return element.getTs() * 1000L;
                                                     }
                                                 }

                );

        // 分组、开窗、聚合
        //TODO 处理迟到的数据： 窗口再等一会
        // 1.当watermark >= 窗口结束时间的时候，会正常触发计算，但是，不会关闭窗口
        // 2.当watermark >= 窗口结束时间 + 窗口等待时间，会真正的关闭窗口
        // 2.当 窗口结束时间 <= watermark <= 窗口结束时间 + 窗口等待时间,每来一条迟到数据，就会计算一次

        sensorDs.keyBy(data -> data.getId())
                .timeWindow(Time.seconds(5))
                .allowedLateness(Time.seconds(2))
                .process(
                        new ProcessWindowFunction<WaterSensor, Object, String, TimeWindow>() {
                            @Override
                            public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<Object> out) throws Exception {
                                out.collect(elements.spliterator().estimateSize());
                            }
                        }
                ).print();

        env.execute();
    }
}
